package com.dzl.goods.service.impl;

import com.dzl.base.service.BaseServiceImpl;
import com.dzl.config.exception.CommonException;
import com.dzl.goods.dao.WelfareGoodsCategoryMapper;
import com.dzl.goods.pojo.WelfareGoodsCategory;
import com.dzl.goods.pojo.WelfareGoodsCategoryRel;
import com.dzl.goods.pojo.query.WelfareGoodsCategoryQuery;
import com.dzl.goods.pojo.vo.WelfareGoodsCategoryVo;
import com.dzl.goods.service.WelfareGoodsCategoryRelService;
import com.dzl.goods.service.WelfareGoodsCategoryService;
import com.dzl.supplier.pojo.WelfareSupplierBrand;
import com.dzl.supplier.service.WelfareSupplierBrandService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 商品品类表(WelfareGoodsCategory)表服务实现类
 * @author wangjian
 * @date 2021-09-18 09:51:15
 */
@Service
public class WelfareGoodsCategoryServiceImpl extends BaseServiceImpl<WelfareGoodsCategory> implements WelfareGoodsCategoryService {

    @Autowired
    private WelfareGoodsCategoryMapper mapper;
    @Autowired
    private WelfareSupplierBrandService welfareSupplierBrandService;
    @Autowired
    private WelfareGoodsCategoryRelService welfareGoodsCategoryRelService;

    @Override
    public PageInfo<WelfareGoodsCategoryVo> selectCategoryListPage(WelfareGoodsCategoryQuery welfareGoodsCategoryQuery) {
        PageHelper.startPage(welfareGoodsCategoryQuery);
        List<WelfareGoodsCategoryVo> list = mapper.selectCategoryListData(welfareGoodsCategoryQuery);
        PageInfo<WelfareGoodsCategoryVo> page = new PageInfo<WelfareGoodsCategoryVo>(list);
        return page;
    }

    @Override
    public List<WelfareGoodsCategoryVo> selectCategoryListData() {
        return mapper.selectCategoryListData(null);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer deleteCategory(Integer id) {
        //查询该品牌关联商品数量
        WelfareGoodsCategoryRel welfareGoodsCategoryRelQuery = new WelfareGoodsCategoryRel();
        welfareGoodsCategoryRelQuery.setCategoryId(id);
        int welfareGoodsCategoryRelCount =  welfareGoodsCategoryRelService.selectCount(welfareGoodsCategoryRelQuery);
        if(welfareGoodsCategoryRelCount>0){
            throw new CommonException("该分类有关联商品,无法删除");
        }
        WelfareGoodsCategory welfareGoodsCategory = new WelfareGoodsCategory();
        welfareGoodsCategory.setId(id);
        welfareGoodsCategory.setStatus(0);
        mapper.updateByPrimaryKeySelective(welfareGoodsCategory);
        //删除品类品牌供应商关联关系表
        WelfareSupplierBrand welfareSupplierBrand = new WelfareSupplierBrand();
        welfareSupplierBrand.setCategoryId(id);
        welfareSupplierBrandService.delete(welfareSupplierBrand);
        //删除品类商品关联关系
        WelfareGoodsCategoryRel welfareGoodsCategoryRelDeleteParam = new WelfareGoodsCategoryRel();
        welfareGoodsCategoryRelDeleteParam.setCategoryId(id);
        welfareGoodsCategoryRelService.delete(welfareGoodsCategoryRelDeleteParam);
        return 1;
    }
}